home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / util / cdity / ModeProSrc.lha / Prefs / PaletteReq.c < prev    next >
C/C++ Source or Header  |  2000-09-17  |  53KB  |  1,802 lines

  1. #include "mpp.h"
  2. #include "palettereq.h"//                                                                              (4.64.10) (09/17/00)
  3. #include <clib/extras_protos.h>
  4. #include <TagItemMacros.h>
  5. #include <exec/memory.h>
  6. #include <math.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9.  
  10. #include <extras/ext_text.h>
  11. #include <extras/layoutgt.h>
  12. #include "palettereq.h"//                                                                              (4.64.9) (09/17/00)
  13. #include <datatypes/pictureclass.h>
  14.  
  15. #include <proto/asl.h>
  16. #include <proto/dos.h>
  17. #include <proto/diskfont.h>
  18. #include <proto/exec.h>
  19. #include <proto/gadtools.h>
  20. #include <proto/graphics.h>
  21. #include <proto/intuition.h>
  22. #include <proto/iffparse.h>
  23. #include <proto/utility.h>
  24.  
  25. #define PRFLAG_OBTAINPENS       (1<<1)
  26. #define PRFLAG_USER_REDBITS     (1<<2)
  27. #define PRFLAG_USER_GREENBITS   (1<<3)
  28. #define PRFLAG_USER_BLUEBITS    (1<<4)
  29.  
  30. /*
  31. #define MSG_M_PROJECT  "Project"
  32. #define MSG_PRG_PALETTE "_Palette"
  33. #define MSG_PRG_RED "_Red:"
  34. #define MSG_PRG_GREEN "_Green:"
  35. #define MSG_PRG_BLUE "_Blue:"
  36. #define MSG_PRG_RESET "Re_set"
  37. #define MSG_PRG_UNDO "_Undo"
  38. #define MSG_PRG_COPY "_Copy"
  39. #define MSG_PRG_SWAP "S_wap"
  40. #define MSG_PRG_SPREAD "Sprea_d"
  41. #define MSG_PRG_OK "_Ok"
  42. #define MSG_PRG_CANCEL "Cance_l"
  43. #define MSG_PRM_LOADPALETTE "Load Palette"
  44. #define MSG_PRM_LOADPALETTE_KEY "L"
  45. #define MSG_PRM_SAVEPALETTE "Save Palette"
  46. #define MSG_PRM_SAVEPALETTE_KEY "S"
  47. #define MSG_PRM_OK "Ok"
  48. #define MSG_PRM_OK_KEY "O"
  49. #define MSG_PRM_CANCEL "Cancel"
  50. #define MSG_PRM_CANCEL_KEY "C"
  51. */
  52.  
  53. //#define GetString(x) (STRPTR)x
  54.  
  55. BOOL pr_ProcessPaletteTags(struct PaletteRequest *PReq, struct TagItem *TagList);
  56. BOOL pr_ObtainPens(struct PaletteRequest *PReq);
  57. void pr_ReleasePens(struct PaletteRequest *PReq);
  58. void pr_PaletteReq(struct PaletteRequest *PReq);
  59.  
  60. struct PaletteRequest *PR_AllocPaletteRequest(Tag Tags, ... )
  61. {
  62.   struct PaletteRequest *preq;
  63.   
  64.   //printf("PR_AllocPaletteRequest()\n");
  65.   
  66.   if(preq=AllocVec(sizeof(*preq),MEMF_PUBLIC|MEMF_CLEAR))
  67.   {
  68.     //printf("  %8lx\n",preq);
  69.     preq->pr_WinLeft=preq->pr_WinTop=preq->pr_WinWidth=preq->pr_WinHeight=-1;
  70.     if(pr_ProcessPaletteTags(preq,(struct TagItem *)&Tags))
  71.     {
  72.       preq->pr_V39=(((struct Library *)IntuitionBase)->lib_Version)>=39;
  73.  
  74.       return(preq);
  75.     }
  76.     PR_FreePaletteRequest(preq);
  77.   }
  78.   return(0);
  79. }
  80.  
  81. void PR_FreePaletteRequest(struct PaletteRequest *PReq)
  82. {
  83.   //printf("PR_FreePaletteRequest %8x\n",PReq);
  84.   if(PReq)
  85.   {
  86.     //printf("pr_Palette %8x\n",PReq->pr_Palette);
  87.     FreeVec(PReq->pr_Palette);
  88.     //printf("pr_FileReq %8x\n",PReq->pr_FileReq);
  89.     //FreeAslRequest(PReq->pr_FileReq);
  90.     FreeVec(PReq);
  91.   }
  92. }
  93.  
  94.  
  95. ULONG PR_DoPaletteRequest(struct PaletteRequest *PReq, Tag Tags, ... )
  96. {
  97.   ULONG retval=0;
  98.   
  99.   //printf("PR_DoPaletteRequest %8x\n",PReq);
  100.   
  101.   if(PReq)
  102.   {
  103.     if(pr_ProcessPaletteTags(PReq,(struct TagItem *)&Tags))
  104.     {
  105.       FreeVec(PReq->pr_Palette);
  106.       
  107.       if(PReq->pr_Palette=AllocVec(sizeof(*PReq->pr_Palette) * PReq->pr_Colors,MEMF_CLEAR))
  108.       {
  109.         if(PReq->pr_UndoPalette=AllocVec(sizeof(*PReq->pr_UndoPalette) * PReq->pr_Colors,MEMF_CLEAR))
  110.         {
  111.           if(PReq->pr_TextFont=OpenDiskFont(PReq->pr_TextAttr))
  112.           {
  113.             if(PReq->pr_ColorTable=AllocVec(sizeof(*PReq->pr_ColorTable) * PReq->pr_Colors,MEMF_CLEAR))
  114.             {
  115.               PReq->pr_Screen=PReq->pr_UserScreen;
  116.               if(pr_ObtainPens(PReq))
  117.               {
  118.                 pr_PaletteReq(PReq);
  119.                 
  120.                 retval=PReq->pr_RetVal;
  121.                 
  122.                 pr_ReleasePens(PReq);          
  123.               }
  124.               FreeVec(PReq->pr_ColorTable);
  125.             }
  126.             CloseFont(PReq->pr_TextFont);
  127.           }
  128.           FreeVec(PReq->pr_UndoPalette);
  129.         }
  130.       }
  131.     }
  132.   }  
  133.   return(retval);
  134. }
  135.  
  136.  
  137. BOOL pr_ProcessPaletteTags(struct PaletteRequest *PReq, struct TagItem *TagList)
  138. {
  139.   struct TagItem *tag,*tstate;
  140.   ULONG data;
  141.   
  142.   //printf("pr_ProcessPaletteTags %8x\n",PReq);
  143.   
  144.   ProcessTagList(TagList,tag,tstate)
  145.   {
  146.     data=tag->ti_Data;
  147.     switch(tag->ti_Tag)
  148.     {
  149.       case PR_Screen:
  150.         //printf("  PR_Screen %8x\n",data);
  151.         PReq->pr_UserScreen=(struct Screen *)data;
  152.         break;
  153.  
  154.       case PR_Window:
  155.         //printf("  PR_Window %8x\n",data);
  156.         PReq->pr_UserScreen=((struct Window *)data)->WScreen;
  157.         PReq->pr_UserWindow=((struct Window *)data);
  158.         break;
  159.    
  160.       case PR_Colors:
  161.         //printf("  PR_Colors %8x\n",data);
  162.         PReq->pr_Colors=data;
  163.         break;
  164.    
  165.       case PR_InitialPalette:
  166.         //printf("  PR_InitialPalette %8x\n",data);
  167.         PReq->pr_InitialPalette=(APTR)data;
  168.         break;
  169.         
  170.       case PR_ObtainPens:
  171.         //printf("  PR_ObtainPens %8x\n",data);
  172.         PReq->pr_Flags|=PRFLAG_OBTAINPENS;
  173.         break;
  174.         
  175.       case PR_ColorTable:
  176.         //printf("  PR_ColorTable %8x\n",data);
  177.         PReq->pr_UserColorTable=(APTR)data;
  178.         break;
  179.         
  180.       case PR_TextAttr:
  181.         //printf("  PR_TextAttr %8x\n",data);
  182.         PReq->pr_TextAttr=(APTR)data;
  183.         break;
  184.         
  185.       case PR_Title:
  186.         PReq->pr_WindowTitle=(APTR)data;
  187.         break;
  188.         
  189.       case PR_RedBits:
  190.         PReq->pr_Flags&=(~PRFLAG_USER_REDBITS);
  191.         if(data)
  192.         {
  193.           PReq->pr_RedBits=data;
  194.           PReq->pr_Flags|=PRFLAG_USER_REDBITS;
  195.         }
  196.         break;
  197.         
  198.       case PR_GreenBits:
  199.         PReq->pr_Flags&=(~PRFLAG_USER_GREENBITS);
  200.         if(data)
  201.         {
  202.           PReq->pr_GreenBits=data;
  203.           PReq->pr_Flags|=PRFLAG_USER_GREENBITS;
  204.         }
  205.         break;
  206.         
  207.       case PR_BlueBits:
  208.         PReq->pr_Flags&=(~PRFLAG_USER_BLUEBITS);
  209.         if(data)
  210.         {
  211.           PReq->pr_BlueBits=data;
  212.           PReq->pr_Flags|=PRFLAG_USER_BLUEBITS;
  213.         }
  214.         break;
  215.         
  216.       case PR_ModeIDRGBBits:
  217.         {
  218.           struct DisplayInfo di;
  219.           
  220.           if(GetDisplayInfoData(0,(UBYTE *)&di,sizeof(di),DTAG_DISP,data))
  221.           {
  222.             if(PReq->pr_V39)
  223.             { 
  224.               PReq->pr_RedBits   =max(di.RedBits,1);
  225.               PReq->pr_GreenBits =max(di.GreenBits,1);
  226.               PReq->pr_BlueBits  =max(di.BlueBits,1);
  227.             }
  228.             else
  229.             {
  230.               // v38 orless
  231.               PReq->pr_RedBits   =PReq->pr_GreenBits =PReq->pr_BlueBits  =4;
  232.             }
  233.             
  234.             PReq->pr_Flags|=PRFLAG_USER_REDBITS|PRFLAG_USER_GREENBITS|PRFLAG_USER_BLUEBITS;
  235.           }
  236.         }
  237.         break;
  238.         
  239.       case PR_InitialLeftEdge:
  240.         PReq->pr_WinLeft=data;
  241.         break;
  242.         
  243.       case PR_InitialTopEdge:
  244.         PReq->pr_WinTop=data;
  245.         break;
  246.         
  247.       case PR_InitialWidth:
  248.         PReq->pr_WinWidth=data;
  249.         break;
  250.       case PR_InitialHeight:
  251.         PReq->pr_WinHeight=data;
  252.         break;
  253.     }
  254.   }
  255.   return(1);
  256. }
  257.  
  258.  
  259.  
  260. BOOL pr_ObtainPens(struct PaletteRequest *PReq)
  261. {
  262.   LONG l,ok;
  263.  
  264.   ok=1;
  265.  
  266.   if(PReq->pr_InitialPalette)
  267.   {
  268.     for(l=0;l<PReq->pr_Colors;l++)
  269.     {
  270.       PReq->pr_Palette[l]=PReq->pr_InitialPalette[l];
  271.     }
  272.   }
  273.  
  274.   if((PReq->pr_Flags & PRFLAG_OBTAINPENS))
  275.   {
  276.     PReq->pr_CMap=PReq->pr_Screen->ViewPort.ColorMap;
  277.     
  278.     for(l=0;l<PReq->pr_Colors && ok;l++)
  279.     {
  280.       LONG pen;
  281.       pen=ObtainPen(PReq->pr_CMap,-1,
  282.               PReq->pr_Palette[l].Red,
  283.               PReq->pr_Palette[l].Green,
  284.               PReq->pr_Palette[l].Blue,
  285.               PEN_EXCLUSIVE);
  286.       if(pen<0)
  287.       {
  288.         ok=0;
  289.       }
  290.       
  291.       PReq->pr_ColorTable[l]=pen;
  292.       
  293. //      //printf("Obtained pen %d %d\n",pen,PReq->pr_ColorTable[l]);
  294.     }
  295.     if(!ok)
  296.     {
  297.       for(;l>=0;l--)
  298.         ReleasePen(PReq->pr_CMap,PReq->pr_ColorTable[l]);
  299.     }
  300.   }    
  301.   else
  302.   {
  303.     if(PReq->pr_UserColorTable)
  304.     {
  305.       for(l=0;l<PReq->pr_Colors;l++)
  306.       {
  307.         PReq->pr_ColorTable[l]=PReq->pr_UserColorTable[l];
  308.       }
  309.     }
  310.     else
  311.     {
  312.       for(l=0;l<PReq->pr_Colors;l++)
  313.       {
  314.         PReq->pr_ColorTable[l]=l;
  315.       }
  316.     }
  317.   }
  318.  
  319.   return((BOOL)ok);
  320. }
  321.  
  322. void pr_ReleasePens(struct PaletteRequest *PReq)
  323. {
  324.   ULONG l;
  325.   
  326.   //printf("pr_ReleasePens PReq=%8x\n",PReq);
  327.   
  328.   if(PReq->pr_Flags & PRFLAG_OBTAINPENS )
  329.   {
  330.     if(PReq->pr_CMap)
  331.     {
  332.       for(l=0;l<PReq->pr_Colors;l++)
  333.       {
  334.         //printf("  %d\n",PReq->pr_ColorTable[l]);
  335.         ReleasePen(PReq->pr_CMap,PReq->pr_ColorTable[l]);
  336.       }
  337.     }
  338.   }
  339.   
  340.   PReq->pr_CMap=0;
  341. }
  342.  
  343. /* Gad IDs */
  344. #define PGD_PALETTE 0
  345. #define PGD_RED     1
  346. #define PGD_GREEN   2
  347. #define PGD_BLUE    3
  348. #define PGD_OK      4
  349. #define PGD_CANCEL  5
  350. #define PGD_RESET   6
  351. #define PGD_UNDO    7
  352. #define PGD_COPY    8
  353. #define PGD_SWAP    9
  354. #define PGD_SPREAD  10
  355.  
  356. /* Project Menu */
  357. #define M_PALETTELOAD       FULLMENUNUM(0,0,NOSUB)
  358. #define M_PALETTESAVE       FULLMENUNUM(0,1,NOSUB)
  359. /* Bar */
  360. #define M_PALETTEOK         FULLMENUNUM(0,3,NOSUB)
  361. #define M_PALETTECANCEL     FULLMENUNUM(0,4,NOSUB)
  362.  
  363.  
  364.  
  365. struct   NewMenu pr_PaletteMenu[]=
  366. {
  367.   {NM_TITLE ,(STRPTR)MSG_M_PROJECT              ,0  ,0,0,0},
  368.   {NM_ITEM  ,(STRPTR)MSG_PRM_LOADPALETTE        ,(STRPTR)MSG_PRM_LOADPALETTE_KEY        ,0,0,(APTR)M_PALETTELOAD},
  369.   {NM_ITEM  ,(STRPTR)MSG_PRM_SAVEPALETTE        ,(STRPTR)MSG_PRM_SAVEPALETTE_KEY        ,0,0,(APTR)M_PALETTESAVE},
  370.   {NM_ITEM  ,NM_BARLABEL                ,0  ,0,0,0},
  371.   {NM_ITEM  ,(STRPTR)MSG_PRM_OK                 ,(STRPTR)MSG_PRM_OK_KEY                 ,0,0,(APTR)M_PALETTEOK},
  372.   {NM_ITEM  ,(STRPTR)MSG_PRM_CANCEL             ,(STRPTR)MSG_PRM_CANCEL_KEY             ,0,0,(APTR)M_PALETTECANCEL},
  373.   {NM_END   ,NULL                       ,0 ,0,0,0}
  374. };
  375.  
  376. #define PRM_NORMAL 0
  377. #define PRM_COPY   1
  378. #define PRM_SWAP   2
  379. #define PRM_SPREAD 3
  380.  
  381. void pr_ProcessPalGads(struct PaletteRequest *PReq, struct IntuiMessage *IMsg);
  382. void pr_ProcessPalKeys(struct PaletteRequest *PReq, struct IntuiMessage *Imsg);
  383. LONG pr_LongestTextLen(struct TextFont *TF, STRPTR String, ... );
  384.  
  385. struct LG_Control *pr_LayoutPaletteGads(struct PaletteRequest *PReq);
  386. void pr_SetPalette(struct PaletteRequest *PReq);
  387. void pr_UpdateSliders(struct PaletteRequest *PReq);
  388. void pr_GetPalette(struct PaletteRequest *PReq);
  389. void pr_SetUndoBuffer(struct PaletteRequest *PReq);
  390. void pr_GetUndoBuffer(struct PaletteRequest *PReq);
  391. ULONG pr_Mix(ULONG A,ULONG B, float Percent);
  392. void pr_ProcessPalMenus(struct PaletteRequest *PReq, struct Menu *MenuStrip, struct IntuiMessage *imsg);
  393.  
  394. void pr_PGD_Cancel (struct PaletteRequest *PReq);
  395. void pr_PGD_Ok     (struct PaletteRequest *PReq);
  396. //void pr_PGD_Palette        (struct PaletteRequest *PReq, LONG Active, BOOL Keyed);
  397. void pr_PGD_PaletteModed(struct PaletteRequest *PReq, LONG Active, BOOL DoModes, BOOL Keyed);
  398. void pr_PGD_Red    (struct PaletteRequest *PReq, LONG Value, BOOL Keyed);
  399. void pr_PGD_Green  (struct PaletteRequest *PReq, LONG Value, BOOL Keyed);
  400. void pr_PGD_Blue   (struct PaletteRequest *PReq, LONG Value, BOOL Keyed);
  401. void pr_PGD_Undo   (struct PaletteRequest *PReq);
  402. void pr_PGD_Reset  (struct PaletteRequest *PReq);
  403. BOOL pr_PGD_Copy   (struct PaletteRequest *PReq);
  404. BOOL pr_PGD_Swap   (struct PaletteRequest *PReq);
  405. BOOL pr_PGD_Spread (struct PaletteRequest *PReq);
  406.  
  407. void pr_DrawPenBox   (struct PaletteRequest *PReq);
  408. void pr_DrawPenRect  (struct PaletteRequest *PReq);
  409.  
  410. BOOL pr_SelectPaletteFile(struct PaletteRequest *PReq, UBYTE *title,UBYTE save,struct Window *Parent);
  411. BOOL pr_ReadCMAP(STRPTR Filename, struct prRGB *CReg, ULONG Colors);
  412. BOOL pr_SaveCMAP(STRPTR Filename, struct prRGB *CReg, ULONG Colors);
  413.  
  414. LONG pr_LongestTextLen(struct TextFont *TF, STRPTR String, ... )
  415. {
  416.   STRPTR *str;
  417.   LONG len=0,n;
  418.   
  419.   str=&String;
  420.   
  421.   while(*str)
  422.   {
  423.     n=gui_StrLength(SL_TextFont     ,TF,//                                                             (4.64.8) (09/17/00)
  424.                 SL_String       ,*str,
  425.                 SL_IgnoreChars  , "_",
  426.                 TAG_DONE);
  427.     len=max(n,len);
  428.     str++;
  429.   }
  430.   
  431.   return(len);
  432. }
  433.  
  434. struct LG_Control *pr_LayoutPaletteGads(struct PaletteRequest *PReq)
  435. {
  436.   WORD width,height,
  437.        sliderlabellen,slidertopedge,slidernumlen,
  438.        buttontopedge,buttonheight,buttonwidth,
  439.        undobuttontopedge, undobuttonwidth,
  440.        editbuttontopedge, editbuttonwidth;
  441.   struct LG_Control *pr_control;
  442.   STRPTR ok,cancel,red,green,blue,reset,undo,copy,swap,spread;
  443.  
  444.   if(PReq->pr_VisualInfo)
  445.   {
  446. //    rp=PReq->pr_Window->RPort; 
  447.     slidernumlen=gui_MaxStrFontLen(PReq->pr_TextFont,4,'0','9');//                                     (4.64.4) (09/17/00)
  448.     buttonheight=PReq->pr_TextAttr->ta_YSize+6;
  449.  
  450.     /* init strings */
  451.     ok      =GetString(MSG_PRG_OK);
  452.     cancel  =GetString(MSG_PRG_CANCEL);
  453.     red     =GetString(MSG_PRG_RED);
  454.     green   =GetString(MSG_PRG_GREEN);
  455.     blue    =GetString(MSG_PRG_BLUE);
  456.     reset   =GetString(MSG_PRG_RESET);
  457.     undo    =GetString(MSG_PRG_UNDO);
  458.     copy    =GetString(MSG_PRG_COPY);
  459.     swap    =GetString(MSG_PRG_SWAP);
  460.     spread  =GetString(MSG_PRG_SPREAD);
  461.     
  462.     /* init lens */
  463.     buttonwidth     =pr_LongestTextLen(PReq->pr_TextFont,ok,cancel,0)+8;
  464.     sliderlabellen  =pr_LongestTextLen(PReq->pr_TextFont,red,green,blue,0)+8;
  465.     undobuttonwidth =pr_LongestTextLen(PReq->pr_TextFont,reset,undo,0)+8;
  466.     editbuttonwidth =pr_LongestTextLen(PReq->pr_TextFont,copy,swap,spread,0)+8;
  467.   
  468.     PReq->pr_MinHeight=buttonheight*6+6+60+20+8;
  469.     PReq->pr_MinWidth =sliderlabellen+slidernumlen+40;
  470.     PReq->pr_MinWidth =max(PReq->pr_MinWidth,buttonwidth*2);
  471.     PReq->pr_MinWidth =max(PReq->pr_MinWidth,undobuttonwidth*2);
  472.     PReq->pr_MinWidth =max(PReq->pr_MinWidth,editbuttonwidth*3)+8;
  473.  
  474.     width=height=0;
  475.   
  476.     if(PReq->pr_Window)
  477.     {
  478.       width =PReq->pr_WinWidth  - PReq->pr_Window->BorderLeft - PReq->pr_Window->BorderRight  - 8;
  479.       height=PReq->pr_WinHeight - PReq->pr_Window->BorderTop  - PReq->pr_Window->BorderBottom - 8 - 20;
  480.     }
  481.     
  482.     width  = max(width,  PReq->pr_MinWidth - 8); 
  483.     height = max(height, PReq->pr_MinHeight - 8 - 20);
  484.  
  485.     PReq->pr_PLeft = PReq->pr_Screen->WBorLeft  + 4;
  486.     PReq->pr_PTop  = PReq->pr_Screen->WBorTop + PReq->pr_Screen->RastPort.TxHeight + 1  + 4;
  487.     PReq->pr_PWidth= width;
  488.     PReq->pr_PHeight= 16;
  489.     
  490.     buttontopedge=height-buttonheight;
  491.     slidertopedge=buttontopedge-6-(buttonheight*3);
  492.     undobuttontopedge=slidertopedge-buttonheight-3;
  493.     editbuttontopedge=undobuttontopedge-buttonheight;
  494.   
  495.   
  496.     undobuttonwidth =max(width/2,undobuttonwidth);
  497.  
  498.     if(PReq->pr_Window)
  499.     {
  500.       pr_control=LG_CreateGadgets( /* init shit */
  501.                                    LG_VisualInfo            ,PReq->pr_VisualInfo,
  502.                                    LG_UseScreenOffsets      ,PReq->pr_Screen,
  503.                                    LG_EraseRemoved          ,FALSE,
  504.                                    LG_BoundsLeft    ,4,
  505.                                    LG_BoundsTop     ,4 + 20,
  506.                                    LG_BoundsWidth   ,width,
  507.                                    LG_BoundsHeight  ,height,
  508.                                    LG_TextAttr      ,PReq->pr_TextAttr,
  509.                                    
  510.                                    /* Palette Gadget */
  511.                                    LG_GadgetKind    ,PALETTE_KIND,
  512.                                    LG_GadgetID      ,PGD_PALETTE,
  513.                                    LG_LeftEdge      ,0,
  514.                                    LG_TopEdge       ,0,
  515.                                    LG_Width         ,LG_REL_WIDTH(0),
  516.                                    LG_Height        ,editbuttontopedge-4,
  517.                                    LG_Flags         ,PLACETEXT_ABOVE,
  518.                                    LG_LabelFlags    ,LGLF_FITLABEL,
  519.                                    LG_GadgetText    ,GetString(MSG_PRG_PALETTE),
  520.                                    LG_GadgetTags    ,6,
  521.                                      GT_Underscore    ,'_',
  522.                                      GTPA_NumColors   ,PReq->pr_Colors,
  523.                                      GTPA_ColorTable  ,PReq->pr_ColorTable,
  524.                                      GTPA_Depth       ,PReq->pr_Screen->RastPort.BitMap->Depth,
  525.                                      GTPA_Color       ,PReq->pr_ColorTable[PReq->pr_ActiveColor],
  526.                                      GTPA_IndicatorWidth,32,
  527.                                    LG_CreateGadget  ,0,
  528.                                    
  529.                                    
  530.                                    /* Slider init */
  531.                                    LG_LeftEdge      ,sliderlabellen,
  532.                                    LG_Width         ,width-sliderlabellen-slidernumlen-8,
  533.                                    LG_Height        ,buttonheight-2,
  534.                                    LG_LabelFlags    ,0,
  535.                                    LG_Flags         ,0,
  536.                                    
  537.                                    /* Red Slider */
  538.                                    LG_GadgetKind    ,SLIDER_KIND,
  539.                                    LG_GadgetID      ,PGD_RED,
  540.                                    LG_TopEdge       ,slidertopedge,
  541.                                    LG_GadgetText    ,red,
  542.                                    LG_GadgetTags    ,9,
  543.                                    GTSL_Level       ,PReq->pr_Palette[PReq->pr_ActiveColor].Red>>24 ,
  544.                                    GTSL_MaxPixelLen ,slidernumlen+1,
  545.                                    GTSL_Min         ,0,
  546.                                    GTSL_Max         ,PReq->pr_RedMax,
  547.                                    GTSL_LevelFormat ,"%3ld",
  548.                                    GTSL_MaxLevelLen ,4,
  549.                                    GTSL_LevelPlace  ,PLACETEXT_RIGHT,
  550.                                    GTSL_Justification ,GTJ_RIGHT,
  551.                                    GT_Underscore    ,'_',
  552.                                    LG_CreateGadget  ,0,
  553.                                    
  554.                                    /* Green Slider */
  555.                                    LG_TopEdge       ,slidertopedge+buttonheight,
  556.                                    LG_GadgetText    ,green,
  557.                                    LG_GadgetTags    ,9,
  558.                                    GTSL_Level       ,PReq->pr_Palette[PReq->pr_ActiveColor].Green>>24 ,
  559.                                    GTSL_MaxPixelLen ,slidernumlen+1,
  560.                                    GTSL_Min         ,0,
  561.                                    GTSL_Max         ,PReq->pr_GreenMax,
  562.                                    GTSL_LevelFormat ,"%3ld",
  563.                                    GTSL_MaxLevelLen ,4,
  564.                                    GTSL_LevelPlace  ,PLACETEXT_RIGHT,
  565.                                    GTSL_Justification ,GTJ_RIGHT,
  566.                                    GT_Underscore    ,'_',
  567.                                    LG_CreateGadget  ,0,
  568.                                    
  569.                                    /* Blue Slider */
  570.                                    LG_TopEdge       ,slidertopedge+buttonheight*2,
  571.                                    LG_GadgetText    ,blue,
  572.                                    LG_GadgetTags    ,9,
  573.                                    GTSL_Level       ,PReq->pr_Palette[PReq->pr_ActiveColor].Blue>>24 ,
  574.                                    GTSL_MaxPixelLen ,slidernumlen+1,
  575.                                    GTSL_Min         ,0,
  576.                                    GTSL_Max         ,PReq->pr_BlueMax,
  577.                                    GTSL_LevelFormat ,"%3ld",
  578.                                    GTSL_MaxLevelLen ,4,
  579.                                    GTSL_LevelPlace  ,PLACETEXT_RIGHT,
  580.                                    GTSL_Justification ,GTJ_RIGHT,
  581.                                    GT_Underscore    ,'_',
  582.                                    LG_CreateGadget  ,0,
  583.                                    
  584.                                    /* Ok Button */
  585.                                    LG_GadgetTags    ,1,
  586.                                    GT_Underscore    ,'_',
  587.                                    LG_GadgetKind    ,BUTTON_KIND,
  588.                                    LG_GadgetText    ,ok,
  589.                                    LG_LeftEdge      ,0,
  590.                                    LG_TopEdge       ,buttontopedge,
  591.                                    LG_Width         ,buttonwidth,
  592.                                    LG_Height        ,buttonheight,
  593.                                    LG_CreateGadget  ,0,
  594.                                    
  595.                                    /* Cancel Button */
  596.                                    LG_LeftEdge      ,LG_REL_RIGHT(-buttonwidth),
  597.                                    LG_GadgetText    ,cancel,
  598.                                    LG_CreateGadget  ,0,
  599.                                    
  600.                                    
  601.                                    LG_HorizCells    ,2,
  602.                                    /* Reset Button */
  603.                                    LG_LeftEdge      ,LG_REL_CELL_LEFTEDGE(0),
  604.                                    LG_TopEdge       ,undobuttontopedge,
  605.                                    LG_Width         ,LG_REL_CELL_WIDTH(1),
  606.                                    LG_Height        ,buttonheight,
  607.                                    LG_GadgetText    ,reset,
  608.                                    LG_CreateGadget  ,0,
  609.                                    
  610.                                    /* Undo button */
  611.                                    LG_LeftEdge      ,LG_REL_CELL_LEFTEDGE(1),
  612.                                    LG_GadgetText    ,undo,
  613.                                    LG_CreateGadget  ,0,
  614.                                    
  615.                                    
  616.                                    LG_HorizCells    ,3,
  617.                                    /* Copy */
  618.                                    LG_LeftEdge      ,LG_REL_CELL_LEFTEDGE(0),
  619.                                    LG_TopEdge       ,editbuttontopedge,
  620.                                    LG_Width         ,LG_REL_CELL_WIDTH(1),
  621.                                    LG_GadgetText    ,copy,
  622.                                    LG_CreateGadget  ,0,
  623.                                    
  624.                                    /* Swap */
  625.                                    LG_LeftEdge      ,LG_REL_CELL_LEFTEDGE(1),
  626.                                    LG_GadgetText    ,swap,
  627.                                    LG_CreateGadget  ,0,
  628.                                    
  629.                                    /* Spread */
  630.                                    LG_LeftEdge      ,LG_REL_CELL_LEFTEDGE(2),
  631.                                    LG_GadgetText    ,spread,
  632.                                    LG_CreateGadget  ,0,
  633.                                    
  634.   
  635.                                    TAG_DONE);
  636.       return(pr_control);
  637.     }
  638.   }
  639.   return(0);
  640. }                              
  641.  
  642. void pr_PaletteReq(struct PaletteRequest *PReq)
  643. {
  644.   WORD oldleft,oldtop,oldwidth,oldheight;
  645.   struct DisplayInfo di;
  646. //  
  647.   struct IntuiMessage *imsg;
  648.  
  649.   oldleft=oldtop=oldwidth=oldheight=-1;
  650.  
  651.   PReq->pr_PalGo=TRUE;
  652.   PReq->pr_ActiveColor=0;
  653.   PReq->pr_Mode=0;
  654.  
  655.   if(GetDisplayInfoData(0,(UBYTE *)&di,sizeof(di),DTAG_DISP,GetVPModeID(&PReq->pr_Screen->ViewPort)))
  656.   {
  657.     if(PReq->pr_V39)
  658.     {
  659.       if(!(PReq->pr_Flags & PRFLAG_USER_REDBITS))
  660.         PReq->pr_RedBits   =di.RedBits;
  661.       if(!(PReq->pr_Flags & PRFLAG_USER_GREENBITS))
  662.         PReq->pr_GreenBits =di.GreenBits;
  663.       if(!(PReq->pr_Flags & PRFLAG_USER_BLUEBITS))
  664.         PReq->pr_BlueBits  =di.BlueBits;
  665.     }
  666.     else
  667.     {
  668.       if(!(PReq->pr_Flags & PRFLAG_USER_REDBITS))
  669.         PReq->pr_RedBits   =4;
  670.       if(!(PReq->pr_Flags & PRFLAG_USER_GREENBITS))
  671.         PReq->pr_GreenBits =4;
  672.       if(!(PReq->pr_Flags & PRFLAG_USER_BLUEBITS))
  673.         PReq->pr_BlueBits  =4;
  674.       /**********************************************************
  675.       PReq->pr_RedBits   = sqrt(((di.PaletteRange & 0xf00) >> 8)+1);
  676.       PReq->pr_GreenBits = sqrt(((di.PaletteRange & 0xf0) >> 4)+1);
  677.       PReq->pr_BlueBits  = sqrt(((di.PaletteRange & 0xf )  +1));
  678.       PReq->pr_RedBits   = max(PReq->pr_RedBits      ,1);
  679.       PReq->pr_GreenBits = max(PReq->pr_GreenBits    ,1);
  680.       PReq->pr_BlueBits  = max(PReq->pr_BlueBits     ,1);
  681.       */
  682.     }
  683.   }
  684.   else
  685.   {
  686.     if(PReq->pr_V39)
  687.     {
  688.       if(!(PReq->pr_Flags & PRFLAG_USER_REDBITS))
  689.         PReq->pr_RedBits   =8;
  690.       if(!(PReq->pr_Flags & PRFLAG_USER_GREENBITS))
  691.         PReq->pr_GreenBits =8;
  692.       if(!(PReq->pr_Flags & PRFLAG_USER_BLUEBITS))
  693.         PReq->pr_BlueBits  =8;
  694.     }
  695.     else
  696.     {
  697.       if(!(PReq->pr_Flags & PRFLAG_USER_REDBITS))
  698.         PReq->pr_RedBits   =4;
  699.       if(!(PReq->pr_Flags & PRFLAG_USER_GREENBITS))
  700.         PReq->pr_GreenBits =4;
  701.       if(!(PReq->pr_Flags & PRFLAG_USER_BLUEBITS))
  702.         PReq->pr_BlueBits  =4;
  703.     }
  704.   }
  705.       
  706.   PReq->pr_RedBits   =max(PReq->pr_RedBits,1);
  707.   PReq->pr_GreenBits =max(PReq->pr_GreenBits,1);
  708.   PReq->pr_BlueBits  =max(PReq->pr_BlueBits,1);    
  709.       
  710.   pr_GetPalette(PReq);
  711.   
  712.   pr_SetUndoBuffer(PReq);
  713.     
  714.   PReq->pr_PrevColor=PReq->pr_ActiveColor;
  715.     
  716.   if(PReq->pr_VisualInfo=GetVisualInfo(PReq->pr_Screen,0))
  717.   {
  718.     PReq->pr_RedMax   =(1<<PReq->pr_RedBits  )-1;
  719.     PReq->pr_GreenMax =(1<<PReq->pr_GreenBits)-1;
  720.     PReq->pr_BlueMax  =(1<<PReq->pr_BlueBits )-1;
  721.     
  722.     PReq->pr_RedMult   =255/PReq->pr_RedMax;
  723.     PReq->pr_GreenMult =255/PReq->pr_GreenMax;
  724.     PReq->pr_BlueMult  =255/PReq->pr_BlueMax;
  725.  
  726.     PReq->pr_GadControl=pr_LayoutPaletteGads(PReq);
  727.     //printf("gc = %8lx\n",    PReq->pr_GadControl);
  728.     { 
  729.       if(PReq->pr_Window=OpenWindowTags(0,
  730.               WA_CustomScreen,  PReq->pr_Screen,
  731.               WA_Left,          0,
  732.               WA_Top,           0,
  733.               WA_Width,         1,//                                                                   (4.61.1)
  734.               WA_Height,        1,//                                                                   (4.61.1)
  735.                   
  736.               WA_Title,         PReq->pr_WindowTitle,
  737.  
  738.               WA_MaxWidth,      -1,
  739.               WA_MaxHeight,     -1,
  740.               WA_IDCMP,         PALETTEIDCMP | SLIDERIDCMP | BUTTONIDCMP | 
  741.                                 IDCMP_CLOSEWINDOW|IDCMP_NEWSIZE|IDCMP_VANILLAKEY|IDCMP_MENUPICK|IDCMP_REFRESHWINDOW,
  742.               WA_SizeGadget,    TRUE,
  743.               WA_DragBar,       TRUE,
  744.               WA_DepthGadget,   TRUE,
  745.               WA_CloseGadget,   TRUE,
  746.               WA_SizeBBottom,   TRUE,
  747.               WA_Activate,      TRUE,
  748.               WA_NewLookMenus,  TRUE,
  749.               WA_SimpleRefresh, TRUE,
  750.               TAG_DONE))
  751.       {
  752.         struct Process *proc;
  753.         struct Window *winptr;
  754.     
  755.         if(PReq->pr_WinWidth==-1)
  756.           PReq->pr_WinWidth =PReq->pr_MinWidth  + PReq->pr_Window->BorderLeft + PReq->pr_Window->BorderRight;
  757.         if(PReq->pr_WinHeight==-1)
  758.           PReq->pr_WinHeight=PReq->pr_MinHeight + PReq->pr_Window->BorderTop  + PReq->pr_Window->BorderBottom;
  759.           
  760.         if(PReq->pr_WinLeft==-1)
  761.         {
  762.           if(PReq->pr_UserWindow)
  763.           {
  764.             PReq->pr_WinLeft=(PReq->pr_UserWindow->Width - PReq->pr_WinWidth)/2  + PReq->pr_UserWindow->LeftEdge;
  765.           }
  766.           else
  767.           {
  768.             if(PReq->pr_UserScreen)
  769.             {
  770.               PReq->pr_WinLeft=(PReq->pr_UserScreen->Width - PReq->pr_WinWidth)/2  ;
  771.             }
  772.             else
  773.             {
  774.               PReq->pr_WinLeft=0;
  775.             }
  776.           }
  777.         }
  778.         
  779.         if(PReq->pr_WinTop==-1)
  780.         {
  781.           if(PReq->pr_UserWindow)
  782.           {
  783.             PReq->pr_WinTop=(PReq->pr_UserWindow->Height - PReq->pr_WinHeight)/2 + PReq->pr_UserWindow->TopEdge;
  784.           }
  785.           else
  786.           {
  787.             if(PReq->pr_UserScreen)
  788.             {
  789.               PReq->pr_WinTop=(PReq->pr_UserScreen->Height - PReq->pr_WinHeight)/2 ;
  790.             }
  791.             else
  792.             {
  793.               PReq->pr_WinTop=0;
  794.             }
  795.           }
  796.         }
  797.  
  798.         /* make sure size is legal */
  799.         PReq->pr_WinWidth=max(PReq->pr_WinWidth         ,PReq->pr_MinWidth  + PReq->pr_Window->BorderLeft + PReq->pr_Window->BorderRight);
  800.         PReq->pr_WinHeight=max(PReq->pr_WinHeight       ,PReq->pr_MinHeight + PReq->pr_Window->BorderTop  + PReq->pr_Window->BorderBottom);
  801.         
  802.         ChangeWindowBox(PReq->pr_Window,PReq->pr_WinLeft,PReq->pr_WinTop, PReq->pr_WinWidth ,PReq->pr_WinHeight); 
  803.         
  804.         proc=(struct Process *)FindTask(0);
  805.         winptr=proc->pr_WindowPtr;
  806.         proc->pr_WindowPtr=PReq->pr_Window;
  807.         
  808.         if(PReq->pr_MenuStrip=CreateMenus(pr_PaletteMenu,TAG_END))
  809.         {
  810.           if(LayoutMenus(PReq->pr_MenuStrip,PReq->pr_VisualInfo,
  811.                           GTMN_NewLookMenus,TRUE,
  812.                           TAG_END))
  813.           {
  814.             if(SetMenuStrip(PReq->pr_Window,PReq->pr_MenuStrip))
  815.             {
  816.               pr_SetPalette(PReq);
  817.               
  818.               ////printf("%8x %8x\n",PReq->pr_Window->RPort,PReq->pr_TextFont);
  819.               SetFont(PReq->pr_Window->RPort,PReq->pr_TextFont);
  820.               
  821.               //printf("setfont\n");
  822.               {
  823.                 //printf("Layout\n");
  824.                 pr_DrawPenBox(PReq);
  825.                 pr_UpdateSliders(PReq);
  826.  
  827. //                LG_AddGadgets(PReq->pr_Window,PReq->pr_GadControl);
  828.                 while(PReq->pr_PalGo)
  829.                 {
  830.                   WaitPort(PReq->pr_Window->UserPort);
  831.                   while(imsg=GT_GetIMsg(PReq->pr_Window->UserPort))
  832.                   {
  833.                     switch(imsg->Class)
  834.                     {
  835.                       case IDCMP_REFRESHWINDOW:
  836.                         GT_BeginRefresh(PReq->pr_Window);
  837.                         pr_DrawPenBox(PReq);
  838.                         GT_EndRefresh(PReq->pr_Window,TRUE);
  839.                         break;
  840.                         
  841.                       case IDCMP_CLOSEWINDOW:
  842.                         PReq->pr_RetVal=FALSE;
  843.                         PReq->pr_PalGo=FALSE;
  844.                         break;
  845.                         
  846.                       case IDCMP_NEWSIZE:
  847.                         if(oldleft    !=  PReq->pr_Window->LeftEdge     || 
  848.                            oldtop     !=  PReq->pr_Window->TopEdge      || 
  849.                            oldwidth   !=  PReq->pr_Window->Width        || 
  850.                            oldheight  !=  PReq->pr_Window->Height)
  851.                         {
  852.                           LG_FreeGadgets(PReq->pr_GadControl);
  853.                           
  854.                           WindowLimits(PReq->pr_Window,
  855.                               PReq->pr_MinWidth  + PReq->pr_Window->BorderLeft + PReq->pr_Window->BorderRight,
  856.                               PReq->pr_MinHeight + PReq->pr_Window->BorderTop  + PReq->pr_Window->BorderBottom ,
  857.                               -1,-1);
  858.                           
  859.                           RefreshWindowFrame(PReq->pr_Window);
  860.                           {
  861.                             WORD x1,y1,x2,y2;
  862.                             
  863.                             x1=PReq->pr_Window->BorderLeft;
  864.                             y1=PReq->pr_Window->BorderTop;
  865.                             x2=PReq->pr_Window->Width  -PReq->pr_Window->BorderRight -1;
  866.                             y2=PReq->pr_Window->Height -PReq->pr_Window->BorderBottom-1;
  867.                             if(x2>x1 && y2>y1)
  868.                               EraseRect(PReq->pr_Window->RPort,x1,y1,x2,y2);
  869.                           }
  870.                           
  871.                           PReq->pr_WinLeft=PReq->pr_Window->LeftEdge;
  872.                           PReq->pr_WinTop=PReq->pr_Window->TopEdge;
  873.                           PReq->pr_WinWidth=PReq->pr_Window->Width;
  874.                           PReq->pr_WinHeight=PReq->pr_Window->Height;
  875.                           
  876.                           PReq->pr_GadControl=pr_LayoutPaletteGads(PReq);
  877.                           
  878.                           LG_AddGadgets(PReq->pr_Window,PReq->pr_GadControl);
  879.                           pr_UpdateSliders(PReq);
  880.                           pr_DrawPenBox(PReq);
  881.                         }
  882.                         break;
  883.                       case IDCMP_MOUSEMOVE:
  884.                       case IDCMP_GADGETUP:
  885.                         pr_ProcessPalGads(PReq,imsg);
  886.                         break;
  887.                       case IDCMP_VANILLAKEY:
  888.                         pr_ProcessPalKeys(PReq,imsg);
  889.                         break;
  890.                       case IDCMP_MENUPICK:
  891.                         pr_ProcessPalMenus(PReq,PReq->pr_MenuStrip,imsg);
  892.                         break;            
  893.                     }
  894.                     GT_ReplyIMsg(imsg);
  895.                   }
  896.                 }
  897.                 LG_FreeGadgets(PReq->pr_GadControl);
  898.               }
  899.               ClearMenuStrip(PReq->pr_Window);
  900.             }
  901.           } 
  902.           FreeMenus(PReq->pr_MenuStrip);
  903.         }
  904.         
  905.         PReq->pr_WinLeft  =PReq->pr_Window->LeftEdge;//                                                (4.61.2)
  906.         PReq->pr_WinTop   =PReq->pr_Window->TopEdge;//                                                 (4.61.2)
  907.         
  908.         proc->pr_WindowPtr=winptr;
  909.         CloseWindow(PReq->pr_Window);
  910.       }
  911.       FreeVisualInfo(PReq->pr_VisualInfo);
  912.     }
  913.   }  
  914. }
  915.  
  916.  
  917. void pr_DrawPenBox(struct PaletteRequest *PReq)
  918. {
  919.   DrawBevelBox(PReq->pr_Window->RPort,
  920.               PReq->pr_PLeft,   PReq->pr_PTop,
  921.               PReq->pr_PWidth,  PReq->pr_PHeight ,
  922.                 GTBB_Recessed,     1,
  923.                 GT_VisualInfo,     PReq->pr_VisualInfo,
  924.                 TAG_DONE);
  925.   pr_DrawPenRect(PReq);
  926. }
  927.  
  928. void pr_DrawPenRect(struct PaletteRequest *PReq)
  929. {
  930.   SetAPen (PReq->pr_Window->RPort, PReq->pr_ColorTable[PReq->pr_ActiveColor]);
  931.   RectFill(PReq->pr_Window->RPort, 
  932.                 PReq->pr_PLeft+2,                       PReq->pr_PTop+1,
  933.                 PReq->pr_PLeft+PReq->pr_PWidth-3,       PReq->pr_PTop+PReq->pr_PHeight-2);
  934. }
  935.  
  936.  
  937.  
  938.  
  939. void pr_ProcessPalGads(struct PaletteRequest *PReq, struct IntuiMessage *IMsg)
  940. {
  941.   struct Gadget *gad;
  942.   ULONG code,id;
  943.   
  944.   code=IMsg->Code;
  945.   gad=(struct Gadget *)IMsg->IAddress;
  946.   id=gad->GadgetID;
  947.   
  948.   switch(id)
  949.   {
  950.     case PGD_CANCEL:
  951.       pr_PGD_Cancel(PReq);
  952.       break;
  953.     case PGD_OK:
  954.       pr_PGD_Ok(PReq);
  955.       break;
  956.     case PGD_PALETTE:
  957.       pr_PGD_PaletteModed(PReq,code,1,FALSE);
  958.       break;
  959.     case PGD_RED:
  960.       pr_PGD_Red(PReq,code,FALSE);
  961.       break;
  962.     case PGD_GREEN:
  963.       pr_PGD_Green(PReq,code,FALSE);
  964.       break;
  965.     case PGD_BLUE:
  966.       pr_PGD_Blue(PReq,code,FALSE);
  967.       break;
  968.     case PGD_UNDO:
  969.       pr_PGD_Undo(PReq);
  970.       break;
  971.     case PGD_RESET:
  972.       pr_PGD_Reset(PReq);
  973.       break;
  974.     case PGD_COPY:
  975.       pr_PGD_Copy(PReq);
  976.       break;
  977.     case PGD_SWAP:
  978.       pr_PGD_Swap(PReq);
  979.       break;
  980.     case PGD_SPREAD:
  981.       pr_PGD_Spread(PReq);
  982.       break;
  983.   }
  984. }
  985.  
  986.  
  987. void pr_ProcessPalKeys(struct PaletteRequest *PReq, struct IntuiMessage *Imsg)
  988. {
  989. //  struct LG_RetVal lgrv;
  990.   WORD direction=1;
  991.   ULONG gadid,code;
  992.   
  993.   /*
  994.   lgrv=MatchKey2Gad(Imsg->Code,PReqLetters);
  995.   
  996.   if(lgrv.Char & 1) direction=-1;
  997. */
  998.  
  999.   if(LG_GadForKey(PReq->pr_GadControl,Imsg->Code,&gadid,&code))  
  1000.   {
  1001.     if(code&1) direction=-1;
  1002.     
  1003.     switch(gadid)
  1004.     {
  1005.       case PGD_PALETTE:
  1006.         pr_PGD_PaletteModed(PReq,direction,0,TRUE);
  1007.         break;
  1008.       case PGD_RED:
  1009.         pr_PGD_Red(PReq,direction,TRUE);
  1010.         break;
  1011.       case PGD_GREEN:
  1012.         pr_PGD_Green(PReq,direction,TRUE);
  1013.         break;
  1014.       case PGD_BLUE:
  1015.         pr_PGD_Blue(PReq,direction,TRUE);
  1016.         break;
  1017.       case PGD_RESET:
  1018.         pr_PGD_Reset(PReq);
  1019.         break;
  1020.       case PGD_UNDO:
  1021.         pr_PGD_Undo(PReq);
  1022.         break;
  1023.       case PGD_COPY:
  1024.         if(pr_PGD_Copy(PReq))
  1025.           pr_PGD_PaletteModed(PReq,0,1,TRUE);
  1026.         break;
  1027.       case PGD_SWAP:
  1028.         if(pr_PGD_Swap(PReq))
  1029.           pr_PGD_PaletteModed(PReq,0,1,TRUE);
  1030.         break;
  1031.       case PGD_SPREAD:
  1032.         if(pr_PGD_Spread(PReq))
  1033.           pr_PGD_PaletteModed(PReq,0,1,TRUE);
  1034.         break;
  1035.       case PGD_OK:
  1036.         pr_PGD_Ok(PReq);
  1037.         break;
  1038.       case PGD_CANCEL:
  1039.         pr_PGD_Cancel(PReq);
  1040.         break;
  1041.     }
  1042.   }
  1043. }
  1044.  
  1045. void pr_PGD_Cancel(struct PaletteRequest *PReq)
  1046. {
  1047.   PReq->pr_PalGo=FALSE;
  1048.   PReq->pr_RetVal=0;
  1049. }
  1050.  
  1051. void pr_PGD_Ok(struct PaletteRequest *PReq)
  1052. {
  1053.   PReq->pr_PalGo=FALSE;
  1054.   PReq->pr_RetVal=1;
  1055. }
  1056.  
  1057. /*
  1058. void pr_PGD_Palette(struct PaletteRequest *PReq, LONG Active, BOOL Keyed)
  1059. {
  1060.   LONG l;
  1061.  
  1062.   if(!Keyed)
  1063.   {
  1064.     for(l=0;l<PReq->pr_Colors;l++)
  1065.     {
  1066.       if(PReq->pr_ColorTable[l]==Active)
  1067.       {
  1068.         Active=l;
  1069.         break;
  1070.       }
  1071.     }
  1072.   }
  1073.  
  1074.  
  1075.  
  1076.   if(Keyed)
  1077.     Active+=PReq->pr_ActiveColor;
  1078.  
  1079.  
  1080.   if(Active<0)
  1081.     Active=PReq->pr_Colors-1;
  1082.   else
  1083.     if(Active>=PReq->pr_Colors)
  1084.       Active=0;
  1085.   
  1086.   PReq->pr_ActiveColor=Active;
  1087.   if(Keyed)
  1088.     LG_SetGadgetAttrs(PReq->pr_GadControl,PGD_PALETTE,
  1089.                         GTPA_Color,PReq->pr_ColorTable[Active],
  1090.                         TAG_DONE);
  1091.  
  1092.  
  1093.   pr_UpdateSliders(PReq);
  1094. }
  1095. */
  1096. /*
  1097. void pr_PGD_PaletteModed(struct PaletteRequest *PReq, LONG Active, BOOL Keyed)
  1098. {
  1099.   LONG l;
  1100.  
  1101.   if(!Keyed)
  1102.   {
  1103.     for(l=0;l<PReq->pr_Colors;l++)
  1104.     {
  1105.       if(PReq->pr_ColorTable[l]==Active)
  1106.       {
  1107.         Active=l;
  1108.         break;
  1109.       }
  1110.     }
  1111.   }
  1112.  
  1113.  
  1114.   //printf("Active %d\n",Active);
  1115.   
  1116.   if(Active<0)
  1117.     Active=PReq->pr_Colors-1;
  1118.   else
  1119.     if(Active>=PReq->pr_Colors)
  1120.       Active=0;
  1121.       
  1122.   switch(PReq->pr_Mode)
  1123.   {
  1124.     case PRM_COPY:
  1125.       {
  1126.         ULONG color;
  1127.         
  1128.         pr_SetUndoBuffer(PReq);
  1129.     
  1130.         color=PReq->pr_ModeColor;
  1131.     
  1132.         PReq->pr_Palette[Active].Red    =PReq->pr_Palette[color].Red;
  1133.         PReq->pr_Palette[Active].Green  =PReq->pr_Palette[color].Green;
  1134.         PReq->pr_Palette[Active].Blue   =PReq->pr_Palette[color].Blue;      
  1135.       }
  1136.       break;
  1137.     case PRM_SWAP:
  1138.       {
  1139.         ULONG color,dr,dg,db;
  1140.     
  1141.         pr_SetUndoBuffer(PReq);
  1142.     
  1143.         color=PReq->pr_ModeColor;
  1144.       
  1145.         dr=PReq->pr_Palette[color].Red;
  1146.         dg=PReq->pr_Palette[color].Green;
  1147.         db=PReq->pr_Palette[color].Blue;        
  1148.     
  1149.         PReq->pr_Palette[color].Red    =PReq->pr_Palette[Active].Red;
  1150.         PReq->pr_Palette[color].Green  =PReq->pr_Palette[Active].Green;
  1151.         PReq->pr_Palette[color].Blue   =PReq->pr_Palette[Active].Blue;      
  1152.   
  1153.         PReq->pr_Palette[Active].Red   =dr;
  1154.         PReq->pr_Palette[Active].Green =dg;
  1155.         PReq->pr_Palette[Active].Blue  =db;
  1156.       }
  1157.       break;
  1158.     case PRM_SPREAD:
  1159.       {
  1160.         ULONG l,color,c1,c2,c3,r1,r2,g1,g2,b1,b2;
  1161.         float p;
  1162.         
  1163.         color=PReq->pr_ModeColor;
  1164.         
  1165.         pr_SetUndoBuffer(PReq);
  1166.         
  1167.         if(color>Active)
  1168.         {
  1169.           c1=Active;
  1170.           c2=color;
  1171.         }
  1172.         else
  1173.         {
  1174.           c1=color;
  1175.           c2=Active;
  1176.         }
  1177.         
  1178.         c3=c2-c1;
  1179.         
  1180.         if(c3>1)
  1181.         {
  1182.           r1=PReq->pr_Palette[c1].Red;
  1183.           g1=PReq->pr_Palette[c1].Green;
  1184.           b1=PReq->pr_Palette[c1].Blue;
  1185.           
  1186.           r2=PReq->pr_Palette[c2].Red;
  1187.           g2=PReq->pr_Palette[c2].Green;
  1188.           b2=PReq->pr_Palette[c2].Blue;
  1189.           for(l=c1+1;l<c2;l++)
  1190.           {
  1191.             p=(float)(l-c1)/(float)c3;
  1192.             PReq->pr_Palette[l].Red  =pr_Mix(r1,r2,p);
  1193.             PReq->pr_Palette[l].Green=pr_Mix(g1,g2,p);
  1194.             PReq->pr_Palette[l].Blue =pr_Mix(b1,b2,p);  
  1195.           }
  1196.         }
  1197.       }
  1198.       break;
  1199.   }
  1200.   PReq->pr_Mode=PRM_NORMAL;
  1201.   
  1202.   pr_SetPalette(PReq);
  1203.   pr_PGD_Palette(PReq,Active,Keyed);
  1204. }
  1205. */
  1206.  
  1207. void pr_PGD_PaletteModed(struct PaletteRequest *PReq, LONG Active, BOOL DoModes, BOOL Keyed)
  1208. {
  1209.   LONG l,oactive;
  1210.  
  1211.   if(Keyed)
  1212.   {
  1213.     Active+=PReq->pr_ActiveColor;
  1214.   }
  1215.   else
  1216.   {
  1217.     {
  1218.       for(l=0;l<PReq->pr_Colors;l++)
  1219.       {
  1220.         if(PReq->pr_ColorTable[l]==Active)
  1221.         {
  1222.           Active=l;
  1223.           break;
  1224.         }
  1225.       }
  1226.     }
  1227.   }
  1228.   
  1229.   if(Active<0)
  1230.     Active=PReq->pr_Colors-1;
  1231.   else
  1232.     if(Active>=PReq->pr_Colors)
  1233.       Active=0;
  1234.       
  1235.   PReq->pr_ActiveColor=Active;
  1236.  
  1237. //  DKP("Activr color=%ld\n",Active);
  1238.  
  1239.   if(DoModes)
  1240.   {
  1241.     switch(PReq->pr_Mode)
  1242.     {
  1243.       case PRM_COPY:
  1244.         {
  1245.           ULONG color;
  1246.           
  1247.           pr_SetUndoBuffer(PReq);
  1248.       
  1249.           color=PReq->pr_ModeColor;
  1250.       
  1251.           PReq->pr_Palette[Active].Red    =PReq->pr_Palette[color].Red;
  1252.           PReq->pr_Palette[Active].Green  =PReq->pr_Palette[color].Green;
  1253.           PReq->pr_Palette[Active].Blue   =PReq->pr_Palette[color].Blue;      
  1254.         }
  1255.         pr_SetPalette(PReq);
  1256.         break;
  1257.       case PRM_SWAP:
  1258.         {
  1259.           ULONG color,dr,dg,db;
  1260.       
  1261.           pr_SetUndoBuffer(PReq);
  1262.       
  1263.           color=PReq->pr_ModeColor;
  1264.         
  1265.           dr=PReq->pr_Palette[color].Red;
  1266.           dg=PReq->pr_Palette[color].Green;
  1267.           db=PReq->pr_Palette[color].Blue;        
  1268.       
  1269.           PReq->pr_Palette[color].Red    =PReq->pr_Palette[Active].Red;
  1270.           PReq->pr_Palette[color].Green  =PReq->pr_Palette[Active].Green;
  1271.           PReq->pr_Palette[color].Blue   =PReq->pr_Palette[Active].Blue;      
  1272.     
  1273.           PReq->pr_Palette[Active].Red   =dr;
  1274.           PReq->pr_Palette[Active].Green =dg;
  1275.           PReq->pr_Palette[Active].Blue  =db;
  1276.         }
  1277.         pr_SetPalette(PReq);
  1278.         break;
  1279.       case PRM_SPREAD:
  1280.         {
  1281.           ULONG l,color,c1,c2,c3,r1,r2,g1,g2,b1,b2;
  1282.           float p;
  1283.           
  1284.           color=PReq->pr_ModeColor;
  1285.           
  1286.           pr_SetUndoBuffer(PReq);
  1287.           
  1288.           if(color>Active)
  1289.           {
  1290.             c1=Active;
  1291.             c2=color;
  1292.           }
  1293.           else
  1294.           {
  1295.             c1=color;
  1296.             c2=Active;
  1297.           }
  1298.           
  1299.           c3=c2-c1;
  1300.           
  1301.           if(c3>1)
  1302.           {
  1303.             r1=PReq->pr_Palette[c1].Red;
  1304.             g1=PReq->pr_Palette[c1].Green;
  1305.             b1=PReq->pr_Palette[c1].Blue;
  1306.             
  1307.             r2=PReq->pr_Palette[c2].Red;
  1308.             g2=PReq->pr_Palette[c2].Green;
  1309.             b2=PReq->pr_Palette[c2].Blue;
  1310.             for(l=c1+1;l<c2;l++)
  1311.             {
  1312.               p=(float)(l-c1)/(float)c3;
  1313.               PReq->pr_Palette[l].Red  =pr_Mix(r1,r2,p);
  1314.               PReq->pr_Palette[l].Green=pr_Mix(g1,g2,p);
  1315.               PReq->pr_Palette[l].Blue =pr_Mix(b1,b2,p);  
  1316.             }
  1317.           }
  1318.         }
  1319.         pr_SetPalette(PReq);
  1320.         break;
  1321.     }
  1322.     PReq->pr_Mode=PRM_NORMAL;
  1323.   }
  1324.  
  1325.   if(Keyed)
  1326.     LG_SetGadgetAttrs(PReq->pr_GadControl,PGD_PALETTE,
  1327.                         GTPA_Color,  PReq->pr_ColorTable[Active],
  1328.                         TAG_DONE);
  1329.  
  1330.   pr_UpdateSliders(PReq);
  1331. }
  1332.  
  1333. void pr_PGD_Red(struct PaletteRequest *PReq, LONG Value, BOOL Keyed)
  1334. {
  1335.   ULONG bitval;
  1336.  
  1337.   if(Keyed)
  1338.     Value+=PReq->pr_RedLevel;
  1339.  
  1340.   if(Value>PReq->pr_RedMax)
  1341.     Value=0;
  1342.   else
  1343.     if(Value<0)
  1344.       Value=PReq->pr_RedMax;
  1345.  
  1346.   
  1347.   PReq->pr_RedLevel=Value;
  1348.  
  1349.   if(PReq->pr_PrevColor!=PReq->pr_ActiveColor)
  1350.   {
  1351.     pr_SetUndoBuffer(PReq);
  1352.   }    
  1353.   bitval=Value*PReq->pr_RedMult;
  1354.   bitval=bitval |  bitval << 8 | bitval << 16 | bitval <<24;
  1355.   PReq->pr_Palette[PReq->pr_ActiveColor].Red=bitval;
  1356.   pr_SetPalette(PReq);
  1357.   if(Keyed)
  1358.     pr_UpdateSliders(PReq);
  1359. }
  1360.  
  1361. void pr_PGD_Green(struct PaletteRequest *PReq, LONG Value, BOOL Keyed)
  1362. {  
  1363.   ULONG bitval;
  1364.  
  1365.   if(Keyed)
  1366.     Value+=PReq->pr_GreenLevel;
  1367.  
  1368.   if(Value>PReq->pr_GreenMax)
  1369.     Value=0;
  1370.   else
  1371.     if(Value<0)
  1372.       Value=PReq->pr_GreenMax;
  1373.  
  1374.   PReq->pr_GreenLevel=Value;
  1375.  
  1376.   if(PReq->pr_PrevColor!=PReq->pr_ActiveColor)
  1377.   {
  1378.     pr_SetUndoBuffer(PReq);
  1379.   }
  1380.   bitval=Value*PReq->pr_GreenMult;
  1381.   bitval=bitval |  bitval << 8 | bitval << 16 | bitval <<24;
  1382.   PReq->pr_Palette[PReq->pr_ActiveColor].Green=bitval;
  1383.   pr_SetPalette(PReq);
  1384.   if(Keyed)
  1385.     pr_UpdateSliders(PReq);
  1386. }
  1387.  
  1388. void pr_PGD_Blue(struct PaletteRequest *PReq, LONG Value, BOOL Keyed)
  1389. {  
  1390.   ULONG bitval;
  1391.  
  1392.   if(Keyed)
  1393.     Value+=PReq->pr_BlueLevel;
  1394.  
  1395.   if(Value>PReq->pr_BlueMax)
  1396.     Value=0;
  1397.   else
  1398.     if(Value<0)
  1399.       Value=PReq->pr_BlueMax;
  1400.  
  1401.   PReq->pr_BlueLevel=Value;
  1402.  
  1403.   if(PReq->pr_PrevColor!=PReq->pr_ActiveColor)
  1404.   {
  1405.     pr_SetUndoBuffer(PReq);
  1406.   }
  1407.   bitval=Value*PReq->pr_BlueMult;
  1408.   bitval=bitval |  bitval << 8 | bitval << 16 | bitval <<24;
  1409.   PReq->pr_Palette[PReq->pr_ActiveColor].Blue=bitval;          
  1410.   pr_SetPalette(PReq);
  1411.   if(Keyed)
  1412.     pr_UpdateSliders(PReq);
  1413. }
  1414.  
  1415. void pr_PGD_Undo(struct PaletteRequest *PReq)
  1416. {
  1417.   pr_GetUndoBuffer(PReq);
  1418.   pr_UpdateSliders(PReq);
  1419.   pr_SetPalette(PReq);
  1420. }
  1421.  
  1422. void pr_PGD_Reset(struct PaletteRequest *PReq)
  1423. {
  1424.   pr_GetPalette(PReq);
  1425.   pr_UpdateSliders(PReq);
  1426.   pr_SetPalette(PReq);
  1427. }
  1428.  
  1429. BOOL pr_PGD_Copy(struct PaletteRequest *PReq)
  1430. {
  1431.   if( PReq->pr_Mode!=PRM_COPY)
  1432.   {
  1433.     PReq->pr_ModeColor=PReq->pr_ActiveColor;
  1434.     PReq->pr_Mode=PRM_COPY;
  1435.     return(FALSE);
  1436.   }
  1437.   else
  1438.     return(TRUE);
  1439. }
  1440.  
  1441. BOOL pr_PGD_Swap(struct PaletteRequest *PReq)
  1442. {
  1443.   if( PReq->pr_Mode!=PRM_SWAP)
  1444.   {
  1445.     PReq->pr_ModeColor=PReq->pr_ActiveColor;
  1446.     PReq->pr_Mode=PRM_SWAP;
  1447.      return(FALSE);
  1448.   }
  1449.   else
  1450.     return(TRUE);
  1451. }
  1452. BOOL pr_PGD_Spread(struct PaletteRequest *PReq)
  1453. {
  1454.   if( PReq->pr_Mode!=PRM_SPREAD)
  1455.   {
  1456.     PReq->pr_ModeColor=PReq->pr_ActiveColor;
  1457.     PReq->pr_Mode=PRM_SPREAD;
  1458.    return(FALSE);
  1459.   }
  1460.   else
  1461.     return(TRUE);
  1462. }
  1463.  
  1464. void pr_SetPalette(struct PaletteRequest *PReq)
  1465. {
  1466.   LONG l;
  1467.   
  1468.   if(PReq->pr_V39)
  1469.   {
  1470.     struct lrgb
  1471.     {
  1472.       WORD Colors,Index;
  1473.       LONG Red,Green,Blue;
  1474.       WORD Zero;
  1475.     } lrgb;
  1476.  
  1477.     lrgb.Colors=1;
  1478.     lrgb.Zero=0;
  1479.  
  1480.     for(l=0;l<PReq->pr_Colors;l++)
  1481.     {
  1482.       lrgb.Index=PReq->pr_ColorTable[l];
  1483.       lrgb.Red  =PReq->pr_Palette[l].Red;
  1484.       lrgb.Green=PReq->pr_Palette[l].Green;
  1485.       lrgb.Blue =PReq->pr_Palette[l].Blue;
  1486.       
  1487.       LoadRGB32(&PReq->pr_Window->WScreen->ViewPort,(ULONG *)&lrgb);
  1488.     }
  1489.   }
  1490.   else
  1491.   {
  1492.     UWORD palette;
  1493.     UWORD l;
  1494.     
  1495.     for(l=0;l<PReq->pr_Colors;l++)
  1496.       palette=((PReq->pr_Palette[l].Red   >> 28) & 0xf00) + 
  1497.               ((PReq->pr_Palette[l].Green >> 24) & 0xf0)  + 
  1498.               ((PReq->pr_Palette[l].Blue  >> 28) & 0xf);
  1499.       
  1500.       SetRGB4(&PReq->pr_Screen->ViewPort,PReq->pr_ColorTable[l],
  1501.                     (UBYTE)(PReq->pr_Palette[l].Red   >> 28),
  1502.                     (UBYTE)(PReq->pr_Palette[l].Green >> 28), 
  1503.                     (UBYTE)(PReq->pr_Palette[l].Blue  >> 28) );
  1504.     
  1505.   }
  1506.   pr_DrawPenRect(PReq);  
  1507. }
  1508.  
  1509. void pr_UpdateSliders(struct PaletteRequest *PReq)
  1510. {
  1511.   ULONG pen;
  1512.   
  1513.   pen=PReq->pr_ActiveColor;
  1514.  
  1515.   PReq->pr_RedLevel=PReq->pr_Palette[pen].Red>>(32-PReq->pr_RedBits);  
  1516.   LG_SetGadgetAttrs(PReq->pr_GadControl,PGD_RED,
  1517.                     GTSL_Level,PReq->pr_RedLevel,
  1518.                     TAG_DONE);
  1519.   
  1520.   PReq->pr_GreenLevel=PReq->pr_Palette[pen].Green>>(32-PReq->pr_GreenBits);
  1521.   LG_SetGadgetAttrs(PReq->pr_GadControl,PGD_GREEN,
  1522.                     GTSL_Level,PReq->pr_GreenLevel,
  1523.                     TAG_DONE);
  1524.  
  1525.   PReq->pr_BlueLevel=PReq->pr_Palette[pen].Blue>>(32-PReq->pr_BlueBits);
  1526.   LG_SetGadgetAttrs(PReq->pr_GadControl,PGD_BLUE,
  1527.                     GTSL_Level,PReq->pr_BlueLevel,
  1528.                     TAG_DONE);
  1529.  
  1530.   pr_DrawPenRect(PReq);
  1531. }
  1532.  
  1533. void pr_GetPalette(struct PaletteRequest *PReq)
  1534. {
  1535.   ULONG l;
  1536.   
  1537.  
  1538.   if(PReq->pr_InitialPalette)
  1539.   {
  1540.     for(l=0;l<PReq->pr_Colors;l++)
  1541.       PReq->pr_Palette[l]=PReq->pr_InitialPalette[l];
  1542.   }
  1543.   else
  1544.   { 
  1545.     if(PReq->pr_V39)
  1546.     {
  1547.       for(l=0;l<PReq->pr_Colors;l++)
  1548.       {
  1549.         GetRGB32(PReq->pr_Screen->ViewPort.ColorMap,PReq->pr_ColorTable[l],1,(ULONG *)&PReq->pr_Palette[l]);
  1550.       }
  1551.     }
  1552.     else
  1553.     {
  1554.       ULONG color;
  1555.       struct ColorMap *cm;
  1556.       
  1557.       cm=PReq->pr_Screen->ViewPort.ColorMap;
  1558.       for(l=0;l<PReq->pr_Colors;l++)
  1559.       {
  1560.         color=GetRGB4(cm,PReq->pr_ColorTable[l]);
  1561.         PReq->pr_Palette[l].Red  =(color & 0x00000f00) << 20;
  1562.         PReq->pr_Palette[l].Green=(color & 0xf0)       << 24;
  1563.         PReq->pr_Palette[l].Blue =(color & 0xf)        << 28;
  1564.       }
  1565.     }
  1566.   }
  1567. }
  1568.  
  1569. void pr_SetUndoBuffer(struct PaletteRequest *PReq)
  1570. {
  1571.   ULONG colors;
  1572.   
  1573.   colors=PReq->pr_Colors;
  1574.   
  1575.   CopyMem(PReq->pr_Palette, PReq->pr_UndoPalette, colors * sizeof(struct prRGB));
  1576.   PReq->pr_PrevColor=PReq->pr_ActiveColor;
  1577. }
  1578.  
  1579. void pr_GetUndoBuffer(struct PaletteRequest *PReq)
  1580. {
  1581.   ULONG l;
  1582.   struct prRGB dummy;
  1583.   
  1584.   for(l=0;l<PReq->pr_Colors;l++)
  1585.   {
  1586.     dummy=PReq->pr_UndoPalette[l];
  1587.     PReq->pr_UndoPalette[l]=PReq->pr_Palette[l];
  1588.     PReq->pr_Palette[l]=dummy;
  1589.   }
  1590. }
  1591.  
  1592. ULONG pr_Mix(ULONG A,ULONG B, float Percent)
  1593. {
  1594.   float a,b,diff;
  1595.   
  1596.   a=A;
  1597.   b=B;
  1598.   
  1599.   diff=b-a;
  1600.  
  1601.   a+=diff*Percent;
  1602.  
  1603.   return((ULONG) (a));
  1604. }
  1605.  
  1606.  
  1607.  
  1608. void pr_ProcessPalMenus(struct PaletteRequest *PReq,  struct Menu *MenuStrip, struct IntuiMessage *imsg)
  1609. {
  1610.   UWORD  menunumber,ns;
  1611.   ULONG  menuid;
  1612.   struct MenuItem *mi;
  1613.   
  1614.   menunumber=imsg->Code;
  1615.   mi=ItemAddress(MenuStrip,menunumber);
  1616.   if(mi)
  1617.   do
  1618.   {
  1619.     menuid=(ULONG)(GTMENUITEM_USERDATA(mi));
  1620.     switch(menuid)
  1621.     {
  1622.       case M_PALETTELOAD:
  1623.         if(pr_SelectPaletteFile(PReq,GetString(MSG_PRM_LOADPALETTE),0,PReq->pr_Window))
  1624.         {
  1625. //          CopyMem(PReq->pr_Palette,palette,PReq->pr_Colors*sizeof(struct prRGB));
  1626.           pr_SetUndoBuffer(PReq);
  1627.           if(pr_ReadCMAP(PReq->pr_FileName,PReq->pr_Palette,PReq->pr_Colors))
  1628.           {
  1629.  
  1630. //            CopyMem(palette,PReq->pr_Palette,PReq->pr_Colors*sizeof(struct prRGB));
  1631.             pr_SetPalette(PReq);
  1632.             pr_UpdateSliders(PReq);
  1633.           }
  1634.         }
  1635.         break;
  1636.         
  1637.       case M_PALETTESAVE:
  1638.         if(pr_SelectPaletteFile(PReq,GetString(MSG_PRM_SAVEPALETTE),1,PReq->pr_Window))
  1639.         {
  1640.           if(!pr_SaveCMAP(PReq->pr_FileName,PReq->pr_Palette,PReq->pr_Colors))
  1641.           {
  1642.             LONG error;
  1643. //            UBYTE errstr[91];
  1644.  
  1645.             if(error=IoErr())
  1646.             {
  1647. //              Fault(error,MiscText[SAVE_ERROR],errstr,90);
  1648. //              EZReq(PReq->pr_Window,0,ModePro,errstr,MiscText[OK],0);
  1649.             }
  1650.           }
  1651.         }
  1652.         break;
  1653.         
  1654.       case M_PALETTEOK:
  1655.         PReq->pr_PalGo=FALSE;
  1656.         PReq->pr_RetVal=1;
  1657.         break;
  1658.         
  1659.       case M_PALETTECANCEL:
  1660.         PReq->pr_PalGo=FALSE;
  1661.         PReq->pr_RetVal=0;
  1662.         break;
  1663.       
  1664.     }
  1665.     ns=mi->NextSelect;
  1666.     mi=ItemAddress(MenuStrip,mi->NextSelect);
  1667.   }while(ns!= MENUNULL && mi);
  1668. }
  1669.  
  1670.  
  1671. BOOL pr_SelectPaletteFile(struct PaletteRequest *PReq, UBYTE *title,UBYTE save,struct Window *Parent)
  1672.   WORD width;
  1673.   
  1674.   if(!PReq->pr_FileReq)
  1675.   {
  1676.     width=Parent->Width/2;
  1677.     PReq->pr_FileReq=(struct FileRequester *)AllocAslRequestTags(ASL_FileRequest, 
  1678.                     ASLFR_TextAttr,        PReq->pr_TextAttr,
  1679.                     ASLFR_InitialTopEdge,  Parent->TopEdge+8,
  1680.                     ASLFR_InitialLeftEdge, Parent->LeftEdge+width/2,
  1681.                     ASLFR_InitialWidth,    width,
  1682.                     ASLFR_InitialHeight,   Parent->Height-16,
  1683.                     ASLFR_DoPatterns  ,    TRUE,
  1684.                     ASLFR_InitialPattern,  "#?",
  1685.                     ASLFR_RejectIcons,     TRUE,
  1686.                     ASLFR_PrivateIDCMP,    TRUE,
  1687.                     TAG_DONE); 
  1688.   }
  1689.   if(PReq->pr_FileReq)
  1690.   {
  1691.     if(AslRequestTags(PReq->pr_FileReq, 
  1692.                   ASLFR_Window   ,       Parent,
  1693.                   ASLFR_SleepWindow,     TRUE,
  1694.                   ASLFR_TitleText,       title,
  1695.                   ASLFR_DoSaveMode,      save,
  1696.                   TAG_DONE))
  1697.     {
  1698.       strncpy(PReq->pr_FileName,PReq->pr_FileReq->fr_Drawer,512);
  1699.       if(AddPart(PReq->pr_FileName,PReq->pr_FileReq->fr_File,512))
  1700.         return(TRUE);
  1701.     }   
  1702.   }
  1703.   return(FALSE);
  1704. }
  1705.  
  1706.  
  1707. BOOL pr_ReadCMAP(STRPTR Filename, struct prRGB *CReg, ULONG Colors)
  1708. {
  1709.   struct IFFHandle *iff;
  1710.   struct StoredProperty *sp;
  1711. //  struct BitMapHeader   *bmhd;
  1712.   struct ColorRegister  *cmap;
  1713.   LONG l;
  1714.   
  1715.   LONG error=0;
  1716.   BOOL rv; 
  1717.  
  1718.   rv=0;
  1719.  
  1720.   if(iff=AllocIFF())
  1721.   { 
  1722.     if(iff->iff_Stream = Open (Filename,MODE_OLDFILE))
  1723.     {
  1724.       InitIFFasDOS(iff);
  1725.       if(!(error=OpenIFF(iff,IFFF_READ)) )
  1726.       {
  1727.         if (!(error=PropChunk(iff,ID_ILBM,ID_CMAP)))
  1728.         {
  1729.           if(!(error=StopOnExit(iff,ID_ILBM,ID_FORM)))           
  1730.           {
  1731.             if((error=ParseIFF(iff,IFFPARSE_SCAN))>-3)
  1732.             {
  1733.               if(sp=FindProp(iff,ID_ILBM,ID_CMAP))
  1734.               {
  1735.                 cmap    =(struct ColorRegister *)sp->sp_Data;
  1736.                 Colors  =min(sp->sp_Size/3,Colors);
  1737.                 for(l=0;l<Colors;l++)
  1738.                 {
  1739.                   CReg[l].Red  =cmap[l].red   * 0x01010101;
  1740.                   CReg[l].Green=cmap[l].green * 0x01010101;
  1741.                   CReg[l].Blue =cmap[l].blue  * 0x01010101;
  1742.                 }
  1743.                 rv=1;
  1744.               }
  1745.             }/* end ParseIFF if */        
  1746.           }
  1747.         } /* End Prop MPPR */
  1748.         CloseIFF(iff);
  1749.       }
  1750.       Close(iff->iff_Stream);
  1751.     }
  1752.     FreeIFF(iff);
  1753.   }
  1754.   
  1755.   return(rv);
  1756. }
  1757.  
  1758.  
  1759. BOOL pr_SaveCMAP(STRPTR Filename, struct prRGB *CReg, ULONG Colors)
  1760. {
  1761.   struct ColorRegister cmap[256];
  1762.   struct IFFHandle *iff;   
  1763.   ULONG l;
  1764.   LONG error;
  1765.   
  1766.   SetIoErr(0);
  1767.   
  1768.   error=1;
  1769.   if(iff=AllocIFF())
  1770.   { 
  1771.     if(iff->iff_Stream = Open (Filename,MODE_NEWFILE))
  1772.     {
  1773.       InitIFFasDOS(iff);
  1774.       if( !(error=OpenIFF(iff,IFFF_WRITE)) )
  1775.       {
  1776.         if(!(error=PushChunk(iff,ID_ILBM,ID_FORM, IFFSIZE_UNKNOWN)))
  1777.         {
  1778.           if(!(error=PushChunk(iff,ID_ILBM,ID_CMAP, Colors * sizeof(cmap[0]))))
  1779.           {
  1780.             for(l=0;l<Colors;l++)
  1781.             {
  1782.               cmap[l].red   =CReg[l].Red>>24;
  1783.               cmap[l].green =CReg[l].Green>>24;
  1784.               cmap[l].blue  =CReg[l].Blue>>24;
  1785.             }
  1786.             error=WriteChunkBytes(iff,cmap,Colors * sizeof(cmap[0]));
  1787.           }
  1788.           error|=PopChunk(iff);
  1789.         }
  1790.         CloseIFF(iff);
  1791.       }
  1792.       Close(iff->iff_Stream);
  1793.     }
  1794.     FreeIFF(iff);
  1795.   }
  1796.   if(error<0)
  1797.     return(FALSE);
  1798.   else
  1799.     return(TRUE);
  1800. }
  1801.